//导入后根据transactionOID查看有无错误信息
import httpFetch from "share/httpFetch";
import config from "config";
import errorMessage from "share/errorMessage";
export default {
  /*
下载模板
*/
  downloadTemplate (downloadMethod, downloadUrl, params) {
    return new Promise((resolve, reject) => {
      httpFetch[downloadMethod](`${config.baseUrl}${downloadUrl}`, params, {}, {responseType: 'arraybuffer'})
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  /*
导入模板
*/
  importTemplate (uploadUrl, params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(`${config.baseUrl}${uploadUrl}`, params)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //导入时展示错误信息
  checkError: function (url, transactionOID, page = 0) {
    let path = `${config.baseUrl}${url.replace("${transactionOID}", transactionOID)}${url.indexOf("?") === -1 ? "?" : "&"}page=${page}&size=10`;
    return new Promise(function (resolve, reject) {
      httpFetch.get(path)
        .then(function (res) {
          resolve(res);
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },


  /*
导入时下载错误信息
*/
  exportError (url, transactionOID) {
    return new Promise((resolve, reject) => {
      httpFetch.get(`${config.baseUrl}${url.replace("${transactionOID}", transactionOID)}`, {}, {}, {responseType: 'arraybuffer'})
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //监听上传接口状态
  listenUploadStatus(listenUrl, transactionID) {
    return httpFetch.get(`${listenUrl}/${transactionID}`)
  },
}
